/* 
Kate
 */
function validateFields(){
    return validateName()&&validatePrice()&&validateDescription();
        
        
}
    
function submitForm(){
    if(validateFields()){ 
        document.getElementById("product_form").submit();
    }else{
        alert("Please enter correct data");
    }
}
    
    
function validateName(){
    var valid=true;
    if (document.getElementById("name_input").value==""){
        document.getElementById("name_error").style.display="inline";
        valid=false;
    } else{
        document.getElementById("name_error").style.display="none";      
    }
    if(document.getElementById("name_input").value.length>100){
        document.getElementById("name_length_error").style.display="inline";  
        valid=false;
    }else{
        document.getElementById("name_length_error").style.display="none";  
           
    }
    if(document.getElementById("name_input").value.indexOf("^")!=-1){
        alert("Symbol \"^\" not allowed");

        valid=false;
    }
    
    
    return valid;
    
    
}

    
    
function validatePrice(){
    var valid=true;
    if(document.getElementById("price_input").value==""){
        document.getElementById("price_error").style.display="inline";
        valid=false;
    }else{
        document.getElementById("price_error").style.display="none";
    }
        
        
    if(isNumber(document.getElementById("price_input").value))
    {
        document.getElementById("price_number_error").style.display="none";   
    } else{
        document.getElementById("price_number_error").style.display="inline";
        valid=false;
    } 
        
    return valid;
}
    
function isNumber(str){ 
    var regDot = new RegExp("[\.]+");
    var regFull = new RegExp("^[0-9]{0,5}[\.]?[0-9]{0,2}$");
    var regShort = new RegExp("^[0-9]{0,5}$");
        
    if(regDot.test(str)){
        return (regFull.test(str));
    }else {
        return (regShort.test(str));
    }
}
    
function validateDescription(){
    var valid=true;
    var description=document.getElementById("description").value;
    if(description.length>2000 ){
        document.getElementById("description_error").style.display="inline";  
        valid=false;
    }else{
        document.getElementById("description_error").style.display="none";   
    }
    return valid; 
} 
    


